﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using Dapper;
using ExaminationSys.DataAccess.Entity;
using DapperExtensions;
using SQLinq;
using SQLinq.Dapper;


namespace ExaminationSys.DataAccess.Repository
{
    public class SectionInformationRepository : Repository<SectionInformation>
    {
        public SectionInformationRepository(SqlConnection context)
            : base(context)
        {
            db = context;
        }

        private SqlConnection db;

        public override SectionInformation GetById(dynamic id)
        {
            return base.ObjectSet.Get<SectionInformation>(db, id, null, null);
        }
    }

    public class SectionInformationService
    {
        private SectionInformationService()
        {
        }

        public List<SectionInformation> GetAll()
        {
            using (var db = DataContent.DBServer())
            {
                IUnitOfWork work = new UnitOfWork(db);
                var result = work.SectionInformation.GetList();
                return result.ToList();
            }
        }

        public List<SectionInformation> GetPublishList(string sectionId)
        {
            using (var db = DataContent.DBServer())
            {
                IUnitOfWork work = new UnitOfWork(db);
                var pg = new PredicateGroup { Operator = GroupOperator.And, Predicates = new List<IPredicate>() };
                pg.Predicates.Add(Predicates.Field<SectionInformation>(f => f.Status, Operator.Eq, 1));
                pg.Predicates.Add(Predicates.Field<SectionInformation>(f => f.SectionID, Operator.Eq, sectionId));
                var result = work.SectionInformation.GetList(pg);
                return result.ToList();
            }
        }
        public List<SectionInformation> GetPublishList()
        {
            using (var db = DataContent.DBServer())
            {
                IUnitOfWork work = new UnitOfWork(db);
                var pg = new PredicateGroup { Operator = GroupOperator.And, Predicates = new List<IPredicate>() };
                pg.Predicates.Add(Predicates.Field<SectionInformation>(f => f.Status, Operator.Eq, 1));
                var result = work.SectionInformation.GetList(pg);
                return result.ToList();
            }
        }


        public Guid GetTopParentId(Guid parentId)
        {
            Guid id = parentId;
            using (var db = DataContent.DBServer())
            {
                /*
                 1 1
                 2 1
                 3 2
                 4 2
                 */
                var item = db.Query(from p in new SQLinq<SectionInformation>()
                                    where p.ID == parentId
                                    select p).First();

                //if (item.ID != item.ParentID)
                //{
                //    id = GetTopParentId(item.ParentID);
                //}

                return id;
            }
        }

        public SectionInformation GetBySectionId(Guid SectionId)
        {
            using (var db = DataContent.DBServer())
            {

                var item = db.Query(from p in new SQLinq<SectionInformation>()
                                    where p.SectionID == SectionId
                                    select p).ToList();
                if (item.Count > 0)
                {
                    return item.First();
                }
                else
                {
                    return null;
                }
            }
        }


        public bool CheckNameExists(string name, Guid id, Guid parentId)
        {
            using (var db = DataContent.DBServer())
            {
                //var result = db.Query(from p in new SQLinq<SectionInformation>()
                //                      where p.ID != id && p.Name == name && p.ParentID == parentId
                //                      select p).FirstOrDefault();
                return false; // result != null;
                //var pg = new PredicateGroup { Operator = GroupOperator.And, Predicates = new List<IPredicate>() };
                //pg.Predicates.Add(Predicates.Field<SectionInformation>(f => f.Name, Operator.Eq, name));
                //pg.Predicates.Add(Predicates.Field<SectionInformation>(f => f.ID, Operator.Eq, id, true));
                //IUnitOfWork work = new UnitOfWork(db);
                //var result = work.SectionInformation.GetList(pg).FirstOrDefault();
                //return result != null;
            }
        }


        public SectionInformation Get(Guid id)
        {
            using (var db = DataContent.DBServer())
            {
                IUnitOfWork work = new UnitOfWork(db);
                var result = work.SectionInformation.GetById(id);
                return result;
            }
        }
        public bool Update(SectionInformation entity)
        {
            using (var db = DataContent.DBServer())
            {
                IUnitOfWork work = new UnitOfWork(db);
                return work.SectionInformation.Update(entity);
            }
        }
        public dynamic Insert(SectionInformation entity)
        {
            using (var db = DataContent.DBServer())
            {
                IUnitOfWork work = new UnitOfWork(db);
                return work.SectionInformation.Insert(entity, null);
            }
        }
    }
}